﻿using AutoMapper;
using B.S.Warehouse.Domain.goods;
using B.S.Warehouse.ErrorCord;
using B.S.Warehouse.Write.Api.Application.Command.GoodsCommand;
using MediatR;
using SqlSugar;

namespace B.S.Warehouse.Write.Api.Application.Handelr.GoodsHandler
{
    public class UpdateGoodsHandler : IRequestHandler<UpdateGoodsCommand, ApiResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public UpdateGoodsHandler(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }
        public async Task<ApiResult<int>> Handle(UpdateGoodsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var model = mapper.Map<GoodsModel>(request);
            model.IsDel = false;
            result.data = await db.Updateable<GoodsModel>(model).ExecuteCommandAsync();
            if (result.data > 0)
            {
                result.Code = ApiEnum.修改成功;
                result.MAG = ApiEnum.修改成功.ToString();
            }
            else
            {
                result.Code = ApiEnum.修改成功;
                result.MAG = ApiEnum.修改成功.ToString();
            }
            return result;
        }
    }
}
